package org.jsea.meta.api.service.fmt;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * --.
 * *
 * author   Jiang.hy
 * create   2022-04-11
 */
@Slf4j(topic = "MetaApi")
public class MetaApiValueFormat implements MetaApiFormat {

    protected static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public String format(Object obj) {
        if (obj == null) {
            return null;
        }
        return obj.toString();
    }

    @Override
    public Object parse(String text) {
        try {
            return doparse(text);
        } catch (Exception e) {
            log.error("FormatError : {}", e.getLocalizedMessage(), e);
        }
        return text;
    }

    protected Object doparse(String text) throws Exception {
        return mapper.readValue(text, HashMap.class);
    }
}
